Skip to content

The pipeline archive bundled with the app is mapped too, not read into memory - #1817

Closed
fxd0h wants to merge 1 commit into
seedds:mainfrom
fxd0h:up/map-bundled-archive
Closed

fxd0h wants to merge 1 commit into
seedds:mainfrom
fxd0h:up/map-bundled-archive

Conversation

@fxd0h

@fxd0h fxd0h commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

What and why

arch_open_begin makes a .mtl4a read ask for a mapping, since Foundation reads a file on an external drive into memory instead (A pipeline archive on an external drive is read into memory). pso_bundle_open ran before that window opened, so the app's own Resources/pso/<gpu>.mtl4a was still read whole when the app sits on an external drive. It now opens inside the window. One line, plus a paragraph in that reference entry.

Checked

  • Tests: t_pso_bundle (69), t_pso_archive (135), test_docs.py; host builds for 0400486024DEC000.

  • Ocarina of Time (0400486024DEC000, v131072), app on an external SSD over USB, same save and scene:

    before after
    footprint 19.0 GB 12.2 GB
    Malloc Large 5,032 MB (one 4.8 GB NSData) 135 MB
    frame rate 60 fps, 0 dropped 60 fps, 0 dropped
  • M5 Max, macOS 27.2.

  • No regress.py run: the change only moves when an existing hook is installed, and it reaches only a Mac app with a bundled archive on an external drive. iOS isn't touched (its files are on the internal disk).

…on an external drive Foundation read the whole file into memory (Ocarina: 4.8 GB dirty for the whole run)
@github-actions github-actions Bot added nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests) docs Documentation labels Nov 8, 2026
betogzo added a commit to betogzo/nxnative that referenced this pull request Nov 8, 2026
…dds#1817) and today's results: no flickering specks or pale patches, about 10 GB of memory, and the first-run freezes of mesh pipelines
seedds added a commit that referenced this pull request Nov 9, 2026
…o memory: it opened before the window in which a .mtl4a read asks for a mapping, so on an external drive Foundation read the whole file into memory (Ocarina of Time: footprint 19.0 → 12.2 GB, Malloc Large 5,032 → 135 MB, one 4.8 GB NSData). One window now covers every archive open in pso4_init, the bundled archive and the device's folder (PR #1817)

Co-authored-by: Mariano Abad <1788933+fxd0h@users.noreply.github.com>
@seedds

seedds commented Nov 9, 2026

Copy link
Copy Markdown
Owner

Landed in a1e155a with you as co-author, thanks for the diagnosis and the measurements. Two changes from the PR:

  • One window instead of two: arch_open_begin() now runs before pso_bundle_open and arch_open_end() after the device's pso_archive/ block, so every archive open in pso4_init is mapped, and a later open added there is covered without anyone having to remember the hook. The same files are mapped as with your change.
  • In docs/reference/memory.md the extra bullet is folded into the entry's Fix and Check fields, with your Ocarina numbers under Check.

Tests: t_pso_bundle (69), t_pso_archive (162 + Python test), test_docs.py; host builds for 0400486024DEC000. Not re-measured on an external drive here.

@seedds seedds closed this Nov 9, 2026
Know-It-All-Brother added a commit to Know-It-All-Brother/nxnative that referenced this pull request Nov 9, 2026
Know-It-All-Brother pushed a commit to Know-It-All-Brother/nxnative that referenced this pull request Nov 9, 2026
…o memory: it opened before the window in which a .mtl4a read asks for a mapping, so on an external drive Foundation read the whole file into memory (Ocarina of Time: footprint 19.0 → 12.2 GB, Malloc Large 5,032 → 135 MB, one 4.8 GB NSData). One window now covers every archive open in pso4_init, the bundled archive and the device's folder (PR seedds#1817)

Co-authored-by: Mariano Abad <1788933+fxd0h@users.noreply.github.com>
nobz226 pushed a commit to nobz226/nxnative that referenced this pull request Nov 10, 2026
…o memory: it opened before the window in which a .mtl4a read asks for a mapping, so on an external drive Foundation read the whole file into memory (Ocarina of Time: footprint 19.0 → 12.2 GB, Malloc Large 5,032 → 135 MB, one 4.8 GB NSData). One window now covers every archive open in pso4_init, the bundled archive and the device's folder (PR seedds#1817)

Co-authored-by: Mariano Abad <1788933+fxd0h@users.noreply.github.com>
(cherry picked from commit a1e155a)
Fasen24-AI added a commit to Fasen24-AI/swordboiMacNative that referenced this pull request Nov 10, 2026
…r words), seedds#1817 (bundled archive mapped), DM_NVN_COND_CONSERVATIVE (samples-passed conditions keep drawing: an A/B for frame-time flicker)

Upstream pull requests applied in this commit (their authors' work, brought onto this tree with a three-way merge; the description is in each PR):
- seedds#1813 by uncraftablexd (closed): A 64- or 128-bit raw global load that misses every pool reads 0 in all its words again, as the word loads it replaced did: the vector helper returned 0 for word 0 but never wrote gmem_vec, so words 1..3 read the previous load's words or undefined data; a vector load's follower scan takes only its own LDG's words (chain 6ed0e0a1fc9f)
- seedds#1817 by fxd0h (closed): The pipeline archive bundled with the app is mapped too, not read into memory
seedds added a commit that referenced this pull request Nov 14, 2026
…cold bundled archive opens 3-6x faster (PR #2657)

Metal's open (MTL4ArchiveImpl::loadFromURL) hashes every Metal library in the file on the calling thread: the whole __AIR_DATA segment and __TEXT,__metallib section of each GPU image (2.9 GB of Ocarina of Time's 4.4 GB applegpu_g16s file). With the archive mapped (the mapped reader of PR #1817, kept), a file that is not in memory is read one page fault at a time, about 230 MB/s: the gray screen before the pipeline progress screen on an M4 Max. arch_open now asks for those ranges first, from the load commands arch_file_ok already walks: 8 MB pieces on every core, each mapped on its own, given MADV_WILLNEED and unmapped, so the kernel reads them into the file cache Metal's mapping shares. It is used for the bundled archive, the bundle's prewarm files and the device's folder, all inside the mapped reader's window.

The PR read each piece into an 8 MB heap buffer: as fast, but malloc kept the freed buffers (+99 MB of footprint after the open on a 12-core M6) and a warm open copied every byte again. One mapping of the whole file is ruled out because Metal maps it too (two of a 4.4 GB file do not fit an iPhone's address space); F_RDADVISE was erratic (0.34-12.9 s cold).

Measured, each cold run on a fresh APFS clone on an external drive:
- M4 Max, the PR's version: Ocarina of Time's bundled archive in the game 10.57 / 8.07 s -> 1.63 / 1.53 s
- M6, this version, the host's open path inside the mapped reader's window: Divinity: Original Sin 2's bundled archive (373 MB) 0.99-1.58 s -> 0.34 s cold, warm 92-97 -> 99-103 ms; 16 of Ocarina of Time's device files in parallel (1.8 GB) 0.93-1.26 -> 0.64-0.77 s cold, warm 81-82 -> 91-98 ms; footprint after the open the same
- M6, Fast Fusion booted with hosts built from main and from this change (2 runs a side): the bundled archive (74 MB) 1062 / 856 -> 176 / 145 ms, its prewarm file, device folder and the three together 0.99-1.31 -> 0.38-0.47 s; both at 60 fps past frame 2,400, peak memory 2,423 vs 2,416-2,418 MB, the same one red line on both

Checked: t_pso_bundle (83), t_pso_archive (14), t_pso_prewarm (64), test_docs. Not run: regress.py check (one game booted instead: the change alters how pages reach memory, not what Metal opens), clang-tidy (not installed here), iOS.

Co-authored-by: NicoPV94 <17337435+NicoPV94@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants